Swift Recipes by T. Michael Rogers

Swift Recipes by T. Michael Rogers

Author:T. Michael Rogers
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


Create an NSButton using a CGRect to define its frame:

var button = NSButton(frame: CGRect(x: 100, y: 100, width: 80, height: 30))

Call setbuttonType: using a value from the NSButtonType enumeration. The default style is MomentaryPushInButton:

button.setButtonType(NSButtonType.MomentaryLightButton)

Set the title property to change the text displayed on the button:

button.title = "Click Me!"

The NSButton class uses the target/action pattern to indicate when the button has been clicked. Set the target to the object that is going to handle the click event. Then use a Selector to set the action property:

button.target = self

button.action = Selector("buttonClicked:")

Next, add the button to the window’s contentView property:

self.window.contentView.addSubview(button)

Finally, implement the action method to handle the button click:

func buttonClicked( sender : NSButton ) {

println("Button clicked")

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.